# Extract tract-level 2017 socio-economic demographic variables from Census Bureau's ACS 5-year dataset


# Setup -----
library(tidyverse) 
library(tidycensus)




##############################################################################################
# Download & load FEMA's disaster declarations summaries dataset
# SOURCE: https://www.fema.gov/about/openfema/data-sets, "OpenFEMA Dataset: Disaster Declarations Summaries"
# Download and load data as "DisasterDeclarationsSummaries"


DisasterDeclarationsSummaries = DisasterDeclarationsSummaries %>%
  mutate(
    declarationDate = as.Date(declarationDate),                                                                    # convert to date format
    fipsStateCountyCode = paste0(ifelse(nchar(fipsStateCode)==1, paste0("0", fipsStateCode), fipsStateCode),       # create FIPS state county 5-digit code
                                 
                                 ifelse(nchar(fipsCountyCode)==1, paste0("00", fipsCountyCode),
                                 ifelse(nchar(fipsCountyCode)==2, paste0("0", fipsCountyCode), fipsCountyCode)))
  )




# List of FL counties that declared individual assistance for Hurricane Michael ---------------

Michael_IA_list =  DisasterDeclarationsSummaries %>%    
  filter(ihProgramDeclared==1 | iaProgramDeclared==1) %>%
  filter(declarationTitle=="HURRICANE MICHAEL") %>%
  filter(fipsStateCode==12)




##############################################################################################
# 2017 tract-level socio-economic demographic variables from ACS 5-year 


tract0 <- get_acs(geography = "tract", 
                  variables = c(
                    medincome = "B19013_001", 
                    population ="B01003_001", 
                    
                    poverty_pop_total ="B17017_001",
                    poverty_pop ="B17017_002",
                    white = "B02001_002",
                    Gini = "B19083_001",

                    HousingUnit_Occupied = "B25002_002",
                    HousingUnit_OwnerOccupied = "B25003_002",
                    
                    HousingUnit_Mortgage_Total = "B25027_001",
                    HousingUnit_Mortgage = "B25027_002",
                    
                    pop25 = "B15003_001",
                    Bachelor = "B15003_022",
                    Master = "B15003_023",
                    Professional = "B15003_024",
                    Doctorate = "B15003_025", 
                    
                    population_in_laborforce = "B23025_002",
                    population_unemployed_in_laborforce = "B23025_005"
                  ), 
                  state  = "FL", 
                  year   = 2017,
                  survey = "acs5")


tract0$moe <- NULL
tract2017 = tract0 %>% distinct(GEOID, NAME, .keep_all = F)
for (i in 1:length(unique(tract0$variable))) {
  test_i = tract0[tract0$variable==unique(tract0$variable)[i], "estimate"]
  colnames(test_i)[which(names(test_i) == "estimate")] <- paste0(unique(tract0$variable)[i], "2017")
  tract2017 = cbind.data.frame(tract2017, test_i)
}


tract2017 = tract2017 %>% 
  mutate(GEOID_county = substr(GEOID, 1, 5)) %>%
  filter(GEOID_county %in% Michael_IA_list$fipsStateCountyCode) %>%
  filter(population2017>0) %>%
  filter(!is.na(medincome2017)) %>%
  mutate(
    log_medincome2017     = log(medincome2017),
    log_population2017    = log(population2017),
    ptg_Unemployed2017    = population_unemployed_in_laborforce2017/population_in_laborforce2017,
    ptg_white2017         = white2017/population2017, 
    ptg_Poverty2017       = poverty_pop2017/poverty_pop_total2017,
    ptg_OwnerOccupied2017 = HousingUnit_OwnerOccupied2017/HousingUnit_Occupied2017, 
    ptg_Mortgage2017      = ifelse(HousingUnit_Mortgage_Total2017==0, 0, HousingUnit_Mortgage2017/HousingUnit_Mortgage_Total2017), 
    ptg_Bachelor2017      = (Bachelor2017 + Master2017+ Professional2017 + Doctorate2017)/pop252017,
    
    GEOID = as.numeric(GEOID)
  ) 







save(tract2017 , file="/02_Data/tract2017.RData")


